<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" width="500px" :close-on-click-modal="false" :close-on-press-escape	='false'
               :title="getTitle()">
      <el-form ref="form" :rules="rules" :model="form" label-width="100px" style="margin-top: 30px"  @submit.native.prevent>
        <el-form-item label="文件夹名称" prop="folderName">
          <el-input v-model="form.folderName" size="small" placeholder="请输入文件夹名称" style="width: 300px" @keyup.enter.native="submit"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" align="right">
        <el-button @click="dialogVisible = false" size="mini">取 消</el-button>
        <el-button type="primary" size="mini" @click="submit" @keyup.enter.native="()=>{}">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {addFolder, updateFolder} from "@/api/mail/admin/mail-doc";

    export default {
        name: "MailDocFolder",
        props: ['getFolder'],
        data() {
            return {
                dialogVisible: false,
                form: {
                    id: '',//编辑时的id
                    folderName: '',
                    pid: '',
                    isMake: false
                },
                rules: {
                    folderName: [
                        {required: true, message: '请输入文件夹名称', trigger: 'blur'},
                        {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur'}
                    ]
                }
            }
        },
        methods: {
            init(params) {
                if(this.$refs.form){
                    this.$refs.form.resetFields();
                }
                this.form.id = this.form.folderName = this.form.pid = '';
                if (JSON.stringify(params) !== '{}') { //编辑
                    if (params.isMake) { //编辑
                        this.form.id = params.id;
                        this.form.isMake = params.isMake;
                        this.form.folderName = params.text;
                    } else {//添加子节点
                        this.form.pid = params.id;
                    }
                } else {//新增
                    this.form.isMake = false;
                }
                this.dialogVisible = !this.dialogVisible;
            },
            submit() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.form.id) {
                            if (this.form.isMake) {//编辑
                                updateFolder(this.form).then(res => {
                                    this.dealEnd(res, 'form')
                                }).catch(err => {
                                    console.log(err)
                                })
                            } else {//添加子节点
                                addFolder(this.form).then(res => {
                                    this.dealEnd(res, 'form')
                                }).catch(err => {
                                    console.log(err)
                                })
                            }
                        } else {
                            addFolder(this.form).then(res => {
                                this.dealEnd(res, 'form')
                            }).catch(err => {
                                console.log(err)
                            })
                        }
                    } else {
                        return false;
                    }
                });
            },
            dealEnd(res, formName) {
                if(res.data.success){
                    this.$emit('getFolder');
                    this.dialogVisible = false;
                    this.bus.$emit('refreshFolderChange');
                    this.$refs[formName].resetFields();
                    this.$message({
                        message: res.data.msg,
                        type: 'success'
                    });
                }else{
                    this.$message({
                        type: "success",
                        message: res.data.msg
                    });
                }

            },
            getTitle() {
                if (this.form.pid) {
                    return '添加子文件夹'
                }
                if (this.form.isMake) {
                    return '编辑文件夹'
                }
                if (!this.form.id) {
                    return '新增文件夹'
                }
            }
        }
    }
</script>

<style scoped>

</style>
